<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 学生数据模型
 * 
 * @author 陈蕾
 */
class Model_Member extends ORM
{
	const TABLE = 'member';
	
	// Model table information
    protected $_table_name = 'member';
    protected $_ignored_columns = array('password_confirm');
    
    // Table primary key and value
	protected $_primary_key  = 'id';
	protected $_primary_val  = 'name';
	
	// Validation members
    protected $_filters = array(
    	'name' => array(
    		'htmlspecialchars' => NULL
    	),
    	'sex' => array(
    		'intval' => NULL
    	),
    	'avatar' => array(
    		'intval' => NULL
    	),
    	'mate_avatar' => array(
    		'intval' => NULL
    	),
    	'locked' => array(
    		'intval' => NULL
    	)
    );
    
    /**
     * 数据验证规则
     */
    protected $_rules = array(
        'name'=> array(
			'not_empty'  => NULL,
			'min_length' => array('3'),
			'max_length' => array('20')
        ),
        'password' => array(),
        'email'=> array(
			'not_empty' => NULL,
			'email'     => NULL,
        	'max_length' => array('16')
        ),
        'avatar'=> array(
			'Model_Member::verify_avatar' => NULL
        ),
        'mate_avatar'=> array(
			'Model_Member::verify_avatar' => NULL
        )
    );
	
    public function need_check_password()
    {
    	$this->_rules['password'] = array(
    		'not_empty'  => NULL,
			'min_length' => array('6'),
			'max_length' => array('16')
    	);
    }
    
    /**
     * 登录
     * 
     * @param string $username 登录名
     * @param string $passowrd 密码
     */
	public function login($username, $passowrd)
	{
		$record = $this->where('name', '=', $username)
			->and_where('password', '=', md5($passowrd))
			->find();
		
		if (!$this->loaded()) {
			return FALSE;
		}
        
        // 登录成功, 将当前时间保存为最后登录时间
		$this->last_login = date('Y-m-d H:i:s');
		$this->save();
		return $this->as_array();
	}
	
	/**
	 * 重写后的save
	 * 主要是将password哈希后存储
	 * 
	 * @see modules/orm/classes/kohana/Kohana_ORM::save()
	 */
	public function save()
    {
        if (array_key_exists('password', $this->_changed)) {
            $this->_object['password'] = md5($this->_object['password']);
        }

        return parent::save();
    }
	
	/**
     * 获取数据总数
     */
    public static function getCount()
    {
    	return DB::select(DB::expr('COUNT(*) AS total'))
			->from(self::TABLE)
			->execute()
			->get('total');
    }
    
    /**
     * 数据验证方法
     * 判断用户名是否可用
     */
	public static function username_available(Validate $array, $field)
    {
        if (self::username_exists($array[$field])) {
            $array->error($field, 'username_available', array($array[$field]));
        }
    }
    
     /**
     * 用户名是否已经存在
     */
	public static function username_exists($value)
    {
        return (bool) DB::select(array('COUNT("*")', 'total_count'))
            ->from('member')
            ->where('name', '=', $value)
            ->execute()
            ->get('total_count');
    }
    
    /**
     * 数据验证方法
     * 判断形象标识的值是否合法
     */
    public static function verify_avatar($value)
    {
    	return !empty($value);
    }
}